#!/bin/bash
# author: zw
# date: 2017/8/14

CONSOLETYPE="vt"
if [ -f /sbin/consoletype ]; then
	CONSOLETYPE="$(/sbin/consoletype)"
fi

function toUpperCase() {
	echo $@ | tr '[a-z]' '[A-Z]'
}

function toLowerCase() {
	echo $@ | tr '[A-Z]' '[a-z]'
}

function choicePage() {
	echo
	echo
	if [ "${CONSOLETYPE}" == "pty" ]; then
		echo "============================================="
		echo "           欢迎来到环境包安装向导            "
		echo "============================================="
		echo "选项："
		echo "  1 - 安装以下所有条目（默认）               "
		echo "  2 - 配置环境变量                           "
		echo "  3 - 调整系统配置                           "
		echo "  4 - 配置内存盘                             "
		echo "  5 - 安装第三方动态库                       "
		echo "  6 - 安装基本软件包                         "
		echo "  7 - 安装WEB相关软件包                      "
		echo "  8 - 安装JAVA开发运行环境                   "
		echo "  9 - 安装FTP服务                            "
		echo "  Q - 退出向导                               "
		echo
		echo -n "请选择需要执行的条目[1]："
	else
		echo "============================================="
		echo "            Welcome to ENV Setup             "
		echo "============================================="
		echo "Options:                                     "
		echo "  1 - Install All the below(default)         "
		echo "  2 - Config Environment Variables           "
		echo "  3 - Config System Settings                 "
		echo "  4 - Config Ramdisk                         "
		echo "  5 - Install 3rd Libraries                  "
		echo "  6 - Install Base Software                  "
		echo "  7 - Install Web Software                   "
		echo "  8 - Install JDK & Java 3rd Libs            "
		echo "  9 - Install FTP Service                    "
		echo "  Q - Quit                                   "
		echo
		echo -n "Please choose a number[1]:"
	fi

	read key
	if [ -z $key ]; then
		key=1
	elif [[ $key == "Q" ]] || [[ $key == "q" ]]; then
		echo "Quit"
		exit
	fi
	echo
}

function execScript() {
	script=$1
	shift
	scDir=$(dirname $script)
	scName=$(basename $script)
	cd $scDir
	chmod +x $scName
	./$scName $@
	cd ..
}

function askParam() {
	paramName=$1
	defaultVal=$2
	paramDesc=$3
	if [ "$CONSOLETYPE" == "pty" ]; then
		echo -n "请为 $paramName 设定值"
		if [ "$paramDesc" != "" ]; then
			echo -n "（说明：$paramDesc）"
		fi
		if [ "$defaultVal" != "" ]; then
			echo -n "[$defaultVal]"
		fi
		echo "："
	else
		echo -n "Please set value for $paramName"
		if [ "$paramDesc" != "" ]; then
			echo -n "(P.S.$paramDesc)"
		fi
		if [ "$defaultVal" != "" ]; then
			echo -n "[$defaultVal]"
		fi
		echo ":"
	fi
	read paramVal
	export PARAM_$paramName=$paramVal
}

function installAll() {
	askParam "ROLE" "" "设备角色，取值：ALL,CWS,CS,WS,IS,HSMD,DPS"
	PARAM_ROLE=$(toUpperCase $PARAM_ROLE)
	if [ "$PARAM_ROLE" == "" ]; then
		echo "ROLE is not set"
		exit
	fi
	if [ "$PARAM_ROLE" == "IS" ]; then
		askParam "MEMORY" "" "物理内存大小，取值：16G,32G,64G,128G"
		PARAM_MEMORY=$(toUpperCase $PARAM_MEMORY)
		if [ "$PARAM_MEMORY" == "" ]; then
			echo "MEMORY is not set"
			exit
		fi
	elif [ "$PARAM_ROLE" == "HSMD" ]; then
		askParam "MEMORY" "" "物理内存大小，取值：8G,16G,32G"
		PARAM_MEMORY=$(toUpperCase $PARAM_MEMORY)
		if [ "$PARAM_MEMORY" == "" ]; then
			echo "MEMORY is not set"
			exit
		fi
	fi
	execScript env/setup.sh
	execScript sys/setup.sh
	execScript ramdisk/setup.sh
	execScript libs/setup.sh
	execScript app/setup.sh
	execScript webapp/setup.sh
	execScript java/setup.sh
	execScript ftp/setup.sh

	echo
	echo "All Finished"
	echo "Please [reboot] your computer!!"
	echo
	exit
}

function mainFlow() {
	choicePage

	case $key in
		1)
			installAll
			;;
		2)
			execScript env/setup.sh
			;;
		3)
			execScript sys/setup.sh
			;;
		4)
			execScript ramdisk/setup.sh
			;;
		5)
			execScript libs/setup.sh
			;;
		6)
			execScript app/setup.sh
			;;
		7)
			execScript webapp/setup.sh
			;;
		8)
			execScript java/setup.sh
			;;
		9)
			execScript ftp/setup.sh
			;;
	esac
}

function mainLoop() {
	while (true); do
		mainFlow
	done
}

find -type f -name "*.sh" -exec chmod +x {} \; 

mainLoop

